package com.ssyt.hiring.common.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.service.ForgetPwdService;

/**
 * 忘记密码Servlet
 */
public class ForgetPwdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ForgetPwdService forgetPwdService = new ForgetPwdService();

	public ForgetPwdServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("toInputEmailUI".equals(command)) {
			toInputEmailUI(request, response);
		} else if ("sendEmail".equals(command)) {
			sendEmail(request, response);
		} else if ("toUpdatePasswordUI".equals(command)) {
			toUpdatePasswordUI(request, response);
		} else if ("updatePassword".equals(command)) {
			updatePassword(request, response);
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updatePassword(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String userType = request.getParameter("userType");
		String result = forgetPwdService.updatePassword(userType, email,
				password);
		if (Constants.HIRING_TALENT.equals(result)) {
			request.setAttribute("msg", "您的密码已重置成功，请登录！");
			request.getRequestDispatcher("/WEB-INF/view/talent/login.jsp")
					.forward(request, response);
		} else if (Constants.HIRING_COMPANY.equals(result)) {
			request.setAttribute("msg", "您的密码已重置成功，请登录！");
			request.getRequestDispatcher(
					"/WEB-INF/view/company/company-login.jsp").forward(request,
					response);
		}
	}

	/**
	 * 跳转到修改密码界面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void toUpdatePasswordUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String securityKey = request.getParameter("securityKey");
		String userType = request.getParameter("userType");
		String email = request.getParameter("email");
		// 验证连接是否已过期
		if (forgetPwdService.isEmailLinkExpired(userType, email)) {
			// 验证唯一表示是否正确
			if (forgetPwdService.isMatched(userType, email, securityKey)) {
				request.getRequestDispatcher(
						"/WEB-INF/view/common/update-password.jsp").forward(
						request, response);
			} else {
				request.setAttribute("msg", "连接地址有误或者已过期！");
				request.getRequestDispatcher(
						"/WEB-INF/view/common/forgetPwd-msg.jsp").forward(
						request, response);
			}
		} else {
			request.setAttribute("msg", "连接已过期！");
			request.getRequestDispatcher(
					"/WEB-INF/view/common/forgetPwd-msg.jsp").forward(request,
					response);
		}
	}

	/**
	 * 发送邮件
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void sendEmail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String vcode = (String) request.getSession().getAttribute("vcode");
		String validateCode = request.getParameter("validateCode");
		String userType = request.getParameter("userType");
		String email = request.getParameter("email");
		// 判断验证码是否正确
		if (vcode.equals(validateCode)) {

			// 判断邮箱地址是否存在
			boolean result = forgetPwdService.isExistEmail(userType, email);
			if (result) {
				// 发送邮件,修改连接地址过期时间,修改唯一密匙
				boolean sends = forgetPwdService.sendEmail(email, userType);
				if (sends) {
					request.setAttribute("msg", "邮件已发送至您的邮箱，请查收！");
					request.getRequestDispatcher(
							"/WEB-INF/view/common/forgetPwd-msg.jsp").forward(
							request, response);
				} else {
					request.setAttribute("msg", "邮件发送失败！");
					toInputEmailUI(request, response);
				}
			} else {
				request.setAttribute("msg", "无此邮箱地址！");
				toInputEmailUI(request, response);
			}
		} else {
			request.setAttribute("msg", "验证码输入错误！");
			toInputEmailUI(request, response);
		}
	}

	/**
	 * 跳转到输入邮箱界面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void toInputEmailUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/common/inputEmail.jsp")
				.forward(request, response);
	}
}
